package com.java110.mall.goods.bmo.productCategory.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.product.ProductCategoryDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.mall.goods.bmo.productCategory.IGetProductCategoryBMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IProductCategoryInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getProductCategoryBMOImpl")
public class GetProductCategoryBMOImpl implements IGetProductCategoryBMO {

    @Autowired
    private IProductCategoryInnerServiceSMO productCategoryInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    /**
     * @param productCategoryDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ProductCategoryDto productCategoryDto) {


        int count = productCategoryInnerServiceSMOImpl.queryProductCategorysCount(productCategoryDto);

        List<ProductCategoryDto> productCategoryDtos = null;
        if (count > 0) {
            productCategoryDtos = productCategoryInnerServiceSMOImpl.queryProductCategorys(productCategoryDto);
        } else {
            productCategoryDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) productCategoryDto.getRow()), count, productCategoryDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 查询 目录产品
     *
     * @param productCategoryDto
     * @return
     */
    @Override
    public ResponseEntity<String> queryCategoryProducts(ProductCategoryDto productCategoryDto) {
        List<ProductDto> productDtos = null;

        productDtos = productInnerServiceSMOImpl.queryProducts(productCategoryDto);
        freshProductDtos(productDtos, productCategoryDto);
        List<ProductCategoryDto> productCategoryDtos = getProductCategoryDtos(productDtos);

        List<ProductDto> tmpProductDto = null;
        for (ProductCategoryDto tmpProductCategoryDto : productCategoryDtos) {
            tmpProductDto = new ArrayList<>();
            for (ProductDto productDto : productDtos) {
                if (productDto.getCategoryId().equals(tmpProductCategoryDto.getCategoryId())) {
                    tmpProductDto.add(productDto);
                }
            }
            tmpProductCategoryDto.setProductDtos(tmpProductDto);
        }


        return ResultVo.createResponseEntity(productCategoryDtos);
    }


    private void freshProductDtos(List<ProductDto> productDtos, ProductDto productDto) {

        if (ListUtil.isNull(productDtos)) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (ProductDto tmpProductDto : productDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                   continue;
                }

                if(StringUtil.isEmpty(tmpFileRelDto.getFileRealName())){
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if(tmpFileRelDto.getFileRealName().startsWith("http")){
                    url =  tmpFileRelDto.getFileRealName();
                }
                tmpProductDto.setCoverPhoto(url);
            }
        }

        //刷入库存和销量
        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setStoreId(productDtos.get(0).getStoreId());
        productSpecValueDto.setProductIds(productIds.toArray(new String[productIds.size()]));
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductStockAndSales(productSpecValueDto);
        for (ProductDto tmpProduct : productDtos) {
            for (ProductSpecValueDto tmpProdSpecValue : productSpecValueDtos) {
                if (tmpProdSpecValue.getProductId().equals(tmpProduct.getProductId())) {
                    tmpProduct.setSales(tmpProdSpecValue.getSales());
                    tmpProduct.setStock(tmpProdSpecValue.getStock());
                }
            }
        }
    }

    private List<ProductCategoryDto> getProductCategoryDtos(List<ProductDto> productDtos) {

        List<ProductCategoryDto> productCategoryDtos = new ArrayList<>();
        boolean productFlag = false;
        ProductCategoryDto tmpProductCategoryDto = null;
        for (ProductDto productDto : productDtos) {
            productFlag = false;
            for (ProductCategoryDto productCategoryDto : productCategoryDtos) {
                if (productCategoryDto.getCategoryId().equals(productDto.getCategoryId())) {
                    productFlag = true;
                }
            }

            if (productFlag) {
                continue;
            }
            tmpProductCategoryDto = new ProductCategoryDto();
            tmpProductCategoryDto.setCategoryId(productDto.getCategoryId());
            tmpProductCategoryDto.setCategoryName(productDto.getCategoryName());
            productCategoryDtos.add(tmpProductCategoryDto);
        }

        return productCategoryDtos;
    }

}
